package com.pm.business.service.impl;

import java.util.HashMap;
import java.util.List;

import com.pm.business.domain.BidProjectPresonMap;
import com.pm.business.domain.TypePersonDTO;
import com.pm.common.core.domain.entity.SysDictData;
import com.pm.common.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.pm.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.pm.business.domain.PersonCertificate;
import com.pm.business.mapper.ProjectPersonMapper;
import com.pm.business.domain.ProjectPerson;
import com.pm.business.service.IProjectPersonService;

/**
 * 基本资料管理Service业务层处理
 * 
 * @author zxl
 * @date 2022-05-05
 */
@Service
public class ProjectPersonServiceImpl implements IProjectPersonService 
{
    @Autowired
    private ProjectPersonMapper projectPersonMapper;

    /**
     * 查询基本资料管理
     * 
     * @param personId 基本资料管理主键
     * @return 基本资料管理
     */
    @Override
    public ProjectPerson selectProjectPersonByPersonId(Long personId)
    {
        return projectPersonMapper.selectProjectPersonByPersonId(personId);
    }
    @Override
    public List<ProjectPerson> selectPersonByType(String personType)
    {
        return projectPersonMapper.selectPersonByType(personType);
    }
    @Override
    public Long selectAuditorId(String personName)
    {
        return projectPersonMapper.selectAuditorId(personName);
    }
    @Override
    public Map<String,List<ProjectPerson> > getPersonTypeMap(ProjectPerson projectPerson)
    {
        Map<String,List<ProjectPerson> >resultMap = new HashMap<>();
        TypePersonDTO dto = new TypePersonDTO();
        List<ProjectPerson>allPersonList = projectPersonMapper.selectProjectPersonList(projectPerson);
        allPersonList.forEach(person -> {
            String personType = person.getPersonType();
            if(personType!=null){
                String[]typeArray = personType.split(",");
                for (String type:typeArray) {
                    List<ProjectPerson> personList = resultMap.get(type);
                    if(personList==null){
                        personList = new ArrayList<>();
                        resultMap.put(type,personList);
                    }
                    personList.add(person);
                }
            }
        });
        return resultMap;
    }
    @Override
    public List<TypePersonDTO>  getCascaderList(){
        List<TypePersonDTO>typePersonList = new ArrayList<>();
        List<SysDictData> personTyps = DictUtils.getDictCache("pm_person_type");
        if(personTyps!=null&& personTyps.size()>0) {
            ProjectPerson condition = new ProjectPerson();
            condition.setStatus("0");
            Map<String, List<ProjectPerson>> personTypeMap = getPersonTypeMap(condition);

            personTyps.forEach(pt->{
                TypePersonDTO tpd = new TypePersonDTO();
                tpd.setValue(pt.getDictValue());
                tpd.setLabel(pt.getDictLabel());
                List<TypePersonDTO>children = new ArrayList<>();
                List<ProjectPerson> ppl = personTypeMap.get(tpd.getValue());
                if(ppl!=null && ppl.size()>0){
                    ppl.forEach(pp ->{
                        TypePersonDTO tempTypePersonDTO = new TypePersonDTO();
                        tempTypePersonDTO.setLabel(pp.getPersonName());
                        tempTypePersonDTO.setValue(pp.getPersonId()+"");
                        children.add(tempTypePersonDTO);
                    });
                }
                tpd.setChildren(children);
                typePersonList.add(tpd);
            });
        }
        return typePersonList;
    }
    @Override
    public List<String> selectPMs()
    {
        return projectPersonMapper.selectPMs();
    }
    @Override
    public List<String> selectAuditor()
    {
        return projectPersonMapper.selectAuditor();
    }

    /**
     * 查询基本资料管理列表
     * 
     * @param projectPerson 基本资料管理
     * @return 基本资料管理
     */
    @Override
    public List<ProjectPerson> selectProjectPersonList(ProjectPerson projectPerson)
    {
        return projectPersonMapper.selectProjectPersonList(projectPerson);
    }

    @Override
    public List<ProjectPerson> selectLockedStatusByPersonIds(List<Long> personIds)
    {
        return projectPersonMapper.selectLockedStatusByPersonIds(personIds);
    }

    /**
     * 新增基本资料管理
     * 
     * @param projectPerson 基本资料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertProjectPerson(ProjectPerson projectPerson)
    {
        int rows = projectPersonMapper.insertProjectPerson(projectPerson);
        insertPersonCertificate(projectPerson);
        return rows;
    }

    /**
     * 修改基本资料管理
     * 
     * @param projectPerson 基本资料管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateProjectPerson(ProjectPerson projectPerson)
    {
        projectPersonMapper.deletePersonCertificateByPersonId(projectPerson.getPersonId());
        insertPersonCertificate(projectPerson);
        return projectPersonMapper.updateProjectPerson(projectPerson);
    }

    /**
     * 批量删除基本资料管理
     * 
     * @param personIds 需要删除的基本资料管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProjectPersonByPersonIds(Long[] personIds)
    {
        projectPersonMapper.deletePersonCertificateByPersonIds(personIds);
        return projectPersonMapper.deleteProjectPersonByPersonIds(personIds);
    }
    @Override
    public List<ProjectPerson> selectProjectPersonByPersonIds(Long[] personIds)
    {
        return projectPersonMapper.selectProjectPersonByPersonIds(personIds);
    }

    /**
     * 删除基本资料管理信息
     * 
     * @param personId 基本资料管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteProjectPersonByPersonId(Long personId)
    {
        projectPersonMapper.deletePersonCertificateByPersonId(personId);
        return projectPersonMapper.deleteProjectPersonByPersonId(personId);
    }

    /**
     * 新增证书管理信息
     * 
     * @param projectPerson 基本资料管理对象
     */
    public void insertPersonCertificate(ProjectPerson projectPerson)
    {
        List<PersonCertificate> personCertificateList = projectPerson.getPersonCertificateList();
        Long personId = projectPerson.getPersonId();
        if (StringUtils.isNotNull(personCertificateList))
        {
            List<PersonCertificate> list = new ArrayList<PersonCertificate>();
            for (PersonCertificate personCertificate : personCertificateList)
            {
                personCertificate.setPersonId(personId);
                list.add(personCertificate);
            }
            if (list.size() > 0)
            {
                projectPersonMapper.batchPersonCertificate(list);
            }
        }
    }
    @Override
    public List<ProjectPerson> selectOtherProjectProject(List<Long>personIds,Long projectId){
        return projectPersonMapper.selectOtherProjectProject(personIds,projectId);
    }
    @Override
    public List<ProjectPerson> selectOtherCurrentProject(List<Long>personIds,Long projectId){
        return projectPersonMapper.selectOtherCurrentProject(personIds,projectId);
    }
}
